﻿using System;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using TimeTravel.Components;
using TimeTravel.Extentions;
using TimeTravel.IData;

namespace TimeTravel.SqlServer  
{
    public class MailTemplateData : IMailTemplateData
    {
        #region Fields

        private static readonly SqlHelper DbHelper = new SqlHelper();
        private static readonly string[] Columns = { "[id]", "[title]", "[mail_title]", "[mail_body]"}; 

        #endregion

        #region Implementation of IDataBase<MailTemplateInfo>

        /// <summary>
        /// 添加记录
        /// </summary>
        public int Add(MailTemplateInfo template)
        {
            if (template == null) return 0;
            var strSql = new StringBuilder();
            strSql.Append("insert into MailTemplate (");
            strSql.Append("[title],[mail_title],[mail_body] )");
            strSql.Append(" values ");
            strSql.Append("(@title,@mail_title,@mail_body) ;");
            strSql.Append("select @@IDENTITY;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@title",SqlDbType.NVarChar,50),
                                            new SqlParameter("@mail_title",SqlDbType.NVarChar,50), 
                                            new SqlParameter("@mail_body",SqlDbType.Text,16)
                                        };
            parameters[0].Value = template.Title;
            parameters[1].Value = template.MailTitle;
            parameters[2].Value = template.MailBody;
            var result = DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            if (id == 0) return;
            var strSql = new StringBuilder();
            strSql.Append("update MailTemplate set DeleteFlag = 1 ");
            strSql.Append("where [id] = @id;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = id;
            DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);

        }

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="template"></param>
        public void Update(MailTemplateInfo template)
        {
            if (template == null || template.ID == 0) return;
            var strSql = new StringBuilder();
            strSql.Append("update MailTemplate set ");
            strSql.Append("[title] = @title,");
            strSql.Append("[mail_title] = @mail_title,");
            strSql.Append("[mail_body] = @mail_body ");
            strSql.Append("where [id] = @id ;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@title", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@mail_title", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@mail_body", SqlDbType.Text, 16),
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = template.Title;
            parameters[1].Value = template.MailTitle;
            parameters[2].Value = template.MailBody;
            parameters[3].Value = template.ID;
            DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public IList<MailTemplateInfo> GetAll()
        {
            var strSql = new StringBuilder();
            strSql.AppendFormat("select {0} from MailTemplate ", String.Join(",", Columns));
            strSql.Append("where DeleteFlag = 0;");
            var result = DbHelper.ExecuteObjectList(strSql.ToString(), DataReaderParser);
            return result.ToList();
        }

        /// <summary>
        /// 根据条件获取
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RecordSet<MailTemplateInfo> GetBySearch(PageQueryInfo query)
        {
            if(query == null) return new RecordSet<MailTemplateInfo>();
            return DbHelper.ExecutePaging("MailTemplate", query, DataReaderParser, Columns);
        }

        /// <summary>
        /// 获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MailTemplateInfo GetOne(int id)
        {
            if (id == 0) return null;
            var strSql = new StringBuilder();
            strSql.AppendFormat("select {0} from MailTemplate ", String.Join(",", Columns));
            strSql.Append("where [id] = @id;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = id;
            return DbHelper.ExecuteObject(CommandType.Text, strSql.ToString(), DataReaderParser, parameters);
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        public int GetTotalCount()
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(id) from MailTemplate ");
            strSql.Append("where DeleteFlag = 0;");
            var result = DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString());
            return result == null ? 0 : Convert.ToInt32(result);
        }

        #endregion

        #region BuildNavMenuObject

        /// <summary>
        /// 从DataReader中构建对象
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private MailTemplateInfo DataReaderParser(IDataReader reader)
        {
            var mailTemplate = new MailTemplateInfo
                                   {
                                       ID = reader.GetInt32("id"),
                                       Title = reader.GetString("title"),
                                       MailTitle = reader.GetString("mail_title"),
                                       MailBody = reader.GetString("mail_body")
                                   };
            return mailTemplate;
        }

        #endregion
    }
}

